#!/bin/sh
#
# Use gmake when we can, else use plan old make
#

make=`which gmake`
slash=`echo ${make}|cut -c1`
if test "${slash}" != "/"; then
	make=`which make`
	slash=`echo ${make}|cut -c1`
	if test "${slash}" != "/"; then
		echo $0" : can't find gmake nor make"
		exit 1
	fi
fi

if [ -x ${make} ]; then
	./stop.sh
	cvs -z2 up -dAP
	if [ -x config.status ]; then
		./config.status --recheck # regenerate configuration
		./config.status # regenerate Makefile, autoconf.h
	elif [ -x configure ]; then
		./configure
	else
		echo $0" : You don't have executable config.status nor configure"
	fi
	${make} clean
# make with predefined MAKE_FLAGS environment
	${make} $MAKE_FLAGS
# if we have an executable now
	if [ -x entropy_rsa ]; then
# process and update configuration; save to entropy.conf-new
		./entropy_rsa -sc >entropy_rsa.conf-new
# backup configuration if it exists
		if [ -s entropy_rsa.conf ]; then
			mv -f entropy_rsa.conf entropy_rsa.conf~
		fi 
# install new configuration
		mv entropy_rsa.conf-new entropy_rsa.conf
# run entropy again
		./run.sh
	else
		echo $0" : entropy build failed"
		exit 2
	fi
else
	echo $0" : "${make}" is not executable."
	exit 3
fi # if -x make
exit 0
